<template>
  <div class="profile-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="title">个人中心</h1>
      <p class="subtitle">管理您的个人信息和账户设置</p>
    </div>

    <!-- 主内容卡片 -->
    <el-card class="profile-card" shadow="hover" :body-style="{ padding: '0' }">
      <!-- 个人信息头部区域 -->
      <div class="profile-header">
        <div class="header-bg"></div>
        <div class="avatar-section">
          <div class="avatar-container">
            <el-avatar
              :size="120"
              :src="userStore.userInfo.userAvatar"
              class="user-avatar"
            ></el-avatar>
            <el-upload
              :auto-upload="false"
              :show-file-list="false"
              :on-change="handleAvatarSuccess"
              class="avatar-upload"
            >
              <el-button type="primary" icon="el-icon-camera" size="small" class="upload-btn">
                更换头像
              </el-button>
            </el-upload>
          </div>

          <div class="user-basic">
            <h2 class="user-name">{{ userStore.userInfo.userName }}</h2>
            <p class="user-id" @click="copyUserId">
              <span>ID: {{ userStore.userInfo.id }}</span>
              <el-icon class="copy-icon"><CopyDocument /></el-icon>
            </p>
          </div>
        </div>
      </div>

      <!-- 详细信息区域 -->
      <div class="profile-body">
        <div class="info-grid">
          <div class="info-item">
            <el-icon class="info-icon"><User /></el-icon>
            <div class="info-content">
              <span class="info-label">简介</span>
              <span class="info-value">{{ userStore.userInfo.userProfile || '未设置' }}</span>
            </div>
          </div>

          <div class="info-item">
            <el-icon class="info-icon"><Phone /></el-icon>
            <div class="info-content">
              <span class="info-label">手机号</span>
              <span class="info-value">{{ userStore.userInfo.phone || '未绑定' }}</span>
            </div>
          </div>

          <div class="info-item">
            <el-icon class="info-icon"><Message /></el-icon>
            <div class="info-content">
              <span class="info-label">邮箱</span>
              <span class="info-value">{{ userStore.userInfo.email || '未绑定' }}</span>
            </div>
          </div>
        </div>

        <!-- 编辑区域 -->
        <el-collapse v-model="activeNames" class="edit-collapse" :border="false">
          <el-collapse-item title="编辑个人信息" name="1" class="edit-panel">
            <el-form label-width="100px" :model="editForm" ref="formRef" class="edit-form">
              <el-form-item label="昵称">
                <el-input
                  v-model="editForm.userName"
                  placeholder="请输入昵称"
                  class="form-input"
                ></el-input>
              </el-form-item>
              <el-form-item label="简介">
                <el-input
                  type="textarea"
                  v-model="editForm.userProfile"
                  placeholder="请输入个人简介"
                  :rows="3"
                  class="form-input"
                ></el-input>
              </el-form-item>
              <el-form-item label="手机号">
                <el-input
                  v-model="editForm.phone"
                  placeholder="请输入手机号"
                  class="form-input"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="editForm.email"
                  placeholder="请输入邮箱"
                  class="form-input"
                ></el-input>
              </el-form-item>
              <el-form-item class="form-actions">
                <el-button type="primary" @click="updateUserInfo" class="save-btn">
                  保存修改
                </el-button>
              </el-form-item>
            </el-form>
          </el-collapse-item>
        </el-collapse>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
// 导入Element Plus图标
import { CopyDocument, User, Phone, Message } from '@element-plus/icons-vue'

const userStore = useUserStore()

const activeNames = ref([''])
const editForm = reactive({
  userName: userStore.userInfo.userName,
  userProfile: userStore.userInfo.userProfile,
  phone: userStore.userInfo.phone,
  email: userStore.userInfo.email,
})
const formRef = ref(null)

const copyUserId = async () => {
  try {
    await navigator.clipboard.writeText(userStore.userInfo.id.toString())
    ElMessage.success('ID已复制到剪贴板')
  } catch (err) {
    console.error('无法复制文本: ', err)
  }
}

const handleAvatarSuccess = async (file) => {
  await userStore.updateUserAvatar(file.raw)
  ElMessage.success('头像更新成功')
}

const updateUserInfo = async () => {
  await userStore.updateCurrentUser(editForm)
  ElMessage.success('个人信息更新成功')
}
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  padding: 2rem 1rem;
  background-color: #f5f7fa;
  box-sizing: border-box;
}

.page-header {
  text-align: center;
  margin-bottom: 2rem;
}

.title {
  font-size: 2rem;
  font-weight: 600;
  color: #1d2129;
  margin-bottom: 0.5rem;
}

.subtitle {
  font-size: 1rem;
  color: #86909c;
}

.profile-card {
  max-width: 800px;
  margin: 0 auto;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.profile-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.profile-header {
  position: relative;
}

.header-bg {
  height: 120px;
  background: linear-gradient(135deg, #409eff 0%, #69b1ff 100%);
}

.avatar-section {
  display: flex;
  align-items: center;
  padding: 0 2rem;
  position: relative;
  margin-top: -60px;
}

.avatar-container {
  position: relative;
}

.user-avatar {
  border: 4px solid #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.05);
}

.avatar-upload {
  position: absolute;
  bottom: 0;
  right: 0;
}

.upload-btn {
  border-radius: 50%;
  width: 36px;
  height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  background-color: #fff;
  color: #409eff;
  border-color: #409eff;
}

.user-basic {
  margin-left: 1.5rem;
}

.user-name {
  font-size: 1.5rem;
  font-weight: 600;
  color: #1d2129;
  margin: 0 0 0.5rem 0;
}

.user-id {
  display: flex;
  align-items: center;
  color: #86909c;
  cursor: pointer;
  transition: color 0.2s ease;
  margin: 0;
}

.user-id:hover {
  color: #409eff;
}

.copy-icon {
  margin-left: 0.5rem;
  font-size: 0.9rem;
}

.profile-body {
  padding: 2rem;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #f2f3f5;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 1rem;
  background-color: #f7f8fa;
  border-radius: 8px;
  transition:
    transform 0.2s ease,
    box-shadow 0.2s ease;
}

.info-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.info-icon {
  color: #409eff;
  font-size: 1.25rem;
  margin-right: 1rem;
  width: 24px;
  text-align: center;
}

.info-content {
  flex: 1;
}

.info-label {
  display: block;
  font-size: 0.875rem;
  color: #86909c;
  margin-bottom: 0.25rem;
}

.info-value {
  font-size: 1rem;
  color: #1d2129;
}

.edit-collapse {
  margin-top: 1rem;
}

.edit-panel {
  border-radius: 8px;
  background-color: #f7f8fa;
  overflow: hidden;
}

.edit-form {
  padding: 1.5rem;
}

.form-input {
  width: 100%;
  border-radius: 6px;
  transition:
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.form-input:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 1rem;
}

.save-btn {
  padding: 0.5rem 1.5rem;
  border-radius: 6px;
  font-size: 1rem;
  transition: all 0.2s ease;
}

.save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-container {
    padding: 1rem 0.5rem;
  }

  .avatar-section {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 0 1rem;
  }

  .user-basic {
    margin-left: 0;
    margin-top: 1rem;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .profile-body {
    padding: 1.5rem 1rem;
  }
}
</style>
